//
//  YYEAnswerTableViewCell.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/4.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYETeacMyAnswerTableViewCell.h"

@interface YYETeacMyAnswerTableViewCell ()

@property (nonatomic,strong) UIImageView *backIV;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *questionLabel;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *answerNameLabel;
@property (nonatomic,strong) UIButton *answerBtn;

@end

@implementation YYETeacMyAnswerTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = GeneralBackColor;
        
        self.backIV = [[UIImageView alloc]init];
        self.backIV.image = [UIImage imageNamed:@"noBorder"];
        [self.contentView addSubview:self.backIV];
        self.backIV.userInteractionEnabled = YES;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor blackColor];;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.backIV addSubview:self.titleLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.textColor = [UIColor colorWithHexString:@"#999999"];;
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        [self.backIV addSubview:self.timeLabel];
        
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        [self.backIV addSubview:self.nameLabel];
        
        self.questionLabel = [[UILabel alloc]init];
        self.questionLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        self.questionLabel.font = [UIFont boldSystemFontOfSize:14];
        [self.backIV addSubview:self.questionLabel];
        
        self.answerNameLabel = [[UILabel alloc]init];
        //        self.answerNameLabel.textColor = GeneralBlueColor;
        self.answerNameLabel.font = [UIFont systemFontOfSize:13];
        [self.backIV addSubview:self.answerNameLabel];
        
        self.answerBtn = [[UIButton alloc]init];
        [self.answerBtn setTitleColor:[UIColor colorWithHexString:@"#0062ED"] forState:UIControlStateNormal];
        self.answerBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.backIV addSubview:self.answerBtn];
        self.answerBtn.backgroundColor = [UIColor colorWithHexString:@"#E7F4FC"];
        [self.answerBtn addTarget:self action:@selector(answerAction) forControlEvents:UIControlEventTouchUpInside];
        
        //        self.questionLabel.text = @"为啥我这里没能实现功能？";
        //        self.nameLabel.text = @"Jolin";
        //        self.timeLabel.text = @"2017.7.23";
        //        self.answerNameLabel.text = @"陈晨讲师的回答";
        //        self.answerLabel.text = @"直接运行实现不了，用debug运行~";
        
    }
    return self;
    
}

- (void)answerAction
{
    if (self.block) {
        self.block(_answerModel);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.backIV makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0.5, 0));
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backIV.right).offset(-15);
        make.left.equalTo(self.backIV.left).offset(15);
        make.top.equalTo(self.backIV.top).offset(10);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backIV.right).offset(-16);
        make.top.equalTo(self.backIV.top).offset(31);
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.timeLabel.left).offset(-16);
        make.top.equalTo(self.backIV.top).offset(31);
    }];
    
    [self.questionLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backIV.left).offset(15);
        make.top.equalTo(self.backIV.top).offset(31);
    }];
    

    
    [self.answerBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.bottom).offset(6);
        make.right.equalTo(self.backIV.right).offset(-16);
        //        make.top.equalTo(self.questionLabel.bottom).offset(10);
        make.size.equalTo(CGSizeMake(61, 27));
    }];
    self.answerBtn.layer.masksToBounds = YES;
    self.answerBtn.layer.cornerRadius = 4;
    
    [self.answerNameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backIV.left).offset(10);
        make.right.equalTo(self.answerBtn.left).offset(-10);
        //        make.top.equalTo(self.questionLabel.bottom).offset(10);
        make.bottom.equalTo(self.backIV.bottom).offset(-10);
    }];

    
}

- (void)setAnswerModel:(YYELessonAnswerModel *)answerModel {
    _answerModel = answerModel;
    
    self.titleLabel.text = answerModel.title;
    self.questionLabel.text = answerModel.question;
    self.nameLabel.text = answerModel.student_name;
    self.timeLabel.text = answerModel.que_created_at;
    if (answerModel.answer.length > 0) {
        self.answerNameLabel.hidden = NO;
    
        //        self.answerNameLabel.text = [NSString stringWithFormat:@"%@的回答：",answerModel.teacher_name];
        NSString *answerStr = @"已回答：";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",answerStr,answerModel.answer]];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, answerStr.length)];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(answerStr.length, answerModel.answer.length)];
        self.answerNameLabel.attributedText = attr;

        [self.answerBtn setTitle:@"修改回答" forState:UIControlStateNormal];
    
    }else {
        self.answerNameLabel.hidden = YES;
        [self.answerBtn setTitle:@"回答" forState:UIControlStateNormal];

    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

